home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / WaveTableList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  27.8 KB  |  907 lines  |  [TEXT/KAHL]

  1. /* WaveTableList.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "WaveTableList.h"
  31. #include "StringList.h"
  32. #include "Array.h"
  33. #include "Memory.h"
  34. #include "Alert.h"
  35. #include "DataMunging.h"
  36. #include "PcodeSystem.h"
  37. #include "WaveTableObject.h"
  38. #include "BufferedFileInput.h"
  39. #include "BufferedFileOutput.h"
  40. #include "Files.h"
  41. #include "Scrap.h"
  42.  
  43. struct WaveTableListRec
  44.     {
  45.         StringListRec*                    List;
  46.         struct CodeCenterRec*        CodeCenter;
  47.         struct MainWindowRec*        MainWindow;
  48.         ArrayRec*                                WaveTableArray;
  49.         MyBoolean                                WaveTableListChanged;
  50.     };
  51.  
  52.  
  53. #define MAGICSCRAPSTRING ("\xff\x00\x1f\xfe WaveTableObjectScrap")
  54.  
  55.  
  56. /* create a new wave table list */
  57. WaveTableListRec*        NewWaveTableList(struct MainWindowRec* MainWindow,
  58.                                             struct CodeCenterRec* CodeCenter, WinType* ScreenID,
  59.                                             OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
  60.     {
  61.         WaveTableListRec*    WaveTableList;
  62.  
  63.         WaveTableList = (WaveTableListRec*)AllocPtrCanFail(sizeof(WaveTableListRec),
  64.             "WaveTableListRec");
  65.         if (WaveTableList == NIL)
  66.             {
  67.              FailurePoint1:
  68.                 return NIL;
  69.             }
  70.         WaveTableList->List = NewStringList(ScreenID,XLoc,YLoc,Width,Height,
  71.             GetScreenFont(),9,StringListDontAllowMultipleSelection,"Wave Tables");
  72.         if (WaveTableList->List == NIL)
  73.             {
  74.              FailurePoint2:
  75.                 ReleasePtr((char*)WaveTableList);
  76.                 goto FailurePoint1;
  77.             }
  78.         WaveTableList->WaveTableArray = NewArray();
  79.         if (WaveTableList->WaveTableArray == NIL)
  80.             {
  81.              FailurePoint3:
  82.                 DisposeStringList(WaveTableList->List);
  83.                 goto FailurePoint2;
  84.             }
  85.         WaveTableList->CodeCenter = CodeCenter;
  86.         WaveTableList->MainWindow = MainWindow;
  87.         WaveTableList->WaveTableListChanged = False;
  88.         return WaveTableList;
  89.     }
  90.  
  91.  
  92. /* delete the wave table list and all of the wave tables it contains */
  93. void                                DisposeWaveTableList(WaveTableListRec* WaveTableList)
  94.     {
  95.         long                            Scan;
  96.         long                            Limit;
  97.  
  98.         CheckPtrExistence(WaveTableList);
  99.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  100.         for (Scan = 0; Scan < Limit; Scan += 1)
  101.             {
  102.                 WaveTableObjectRec*    WaveTableTemp;
  103.  
  104.                 WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
  105.                     WaveTableList->WaveTableArray,Scan);
  106.                 DisposeWaveTableObject(WaveTableTemp);
  107.             }
  108.         DisposeArray(WaveTableList->WaveTableArray);
  109.         DisposeStringList(WaveTableList->List);
  110.         ReleasePtr((char*)WaveTableList);
  111.     }
  112.  
  113.  
  114. /* change the location of the wave table list in the window */
  115. void                                SetWaveTableListLocation(WaveTableListRec* WaveTableList,
  116.                                             OrdType XLoc, OrdType YLoc, OrdType Width, OrdType Height)
  117.     {
  118.         CheckPtrExistence(WaveTableList);
  119.         SetStringListLoc(WaveTableList->List,XLoc,YLoc,Width,Height);
  120.     }
  121.  
  122.  
  123. /* redraw the wave table list */
  124. void                                WaveTableListRedraw(WaveTableListRec* WaveTableList)
  125.     {
  126.         CheckPtrExistence(WaveTableList);
  127.         RedrawStringList(WaveTableList->List);
  128.     }
  129.  
  130.  
  131. /* see if the specified coordinates falls inside the wave table list rectangle */
  132. MyBoolean                        WaveTableListHitTest(WaveTableListRec* WaveTableList,
  133.                                             OrdType XLoc, OrdType YLoc)
  134.     {
  135.         CheckPtrExistence(WaveTableList);
  136.         return StringListHitTest(WaveTableList->List,XLoc,YLoc);
  137.     }
  138.  
  139.  
  140. /* handle a mouse down event for the wave table list */
  141. void                                WaveTableListDoMouseDown(WaveTableListRec* WaveTableList,
  142.                                             OrdType XLoc, OrdType YLoc, ModifierFlags Modifiers)
  143.     {
  144.         CheckPtrExistence(WaveTableList);
  145.         if (StringListMouseDown(WaveTableList->List,XLoc,YLoc,Modifiers))
  146.             {
  147.                 /* if it returns true, then it was a double click */
  148.                 WaveTableListOpenSelection(WaveTableList);
  149.             }
  150.     }
  151.  
  152.  
  153. /* called when the window becomes active */
  154. void                                WaveTableListBecomeActive(WaveTableListRec* WaveTableList)
  155.     {
  156.         CheckPtrExistence(WaveTableList);
  157.         EnableStringList(WaveTableList->List);
  158.     }
  159.  
  160.  
  161. /* called when the window becomes inactive */
  162. void                                WaveTableListBecomeInactive(WaveTableListRec* WaveTableList)
  163.     {
  164.         CheckPtrExistence(WaveTableList);
  165.         DisableStringList(WaveTableList->List);
  166.     }
  167.  
  168.  
  169. /* called when a selection is made in another list, so that this list */
  170. /* is deselected */
  171. void                                WaveTableListDeselect(WaveTableListRec* WaveTableList)
  172.     {
  173.         CheckPtrExistence(WaveTableList);
  174.         DeselectAllStringListElements(WaveTableList->List);
  175.     }
  176.  
  177.  
  178. /* check to see if there is a selection in this list */
  179. MyBoolean                        WaveTableListIsThereSelection(WaveTableListRec* WaveTableList)
  180.     {
  181.         CheckPtrExistence(WaveTableList);
  182.         return (GetStringListHowManySelectedItems(WaveTableList->List) > 0);
  183.     }
  184.  
  185.  
  186. /* check to see if any of the wave tables contained in this list need to be saved */
  187. MyBoolean                        DoesWaveTableListNeedToBeSaved(WaveTableListRec* WaveTableList)
  188.     {
  189.         long                            Scan;
  190.         long                            Limit;
  191.         MyBoolean                    Flag;
  192.  
  193.         CheckPtrExistence(WaveTableList);
  194.         Flag = WaveTableList->WaveTableListChanged;
  195.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  196.         for (Scan = 0; (Scan < Limit) && !Flag; Scan += 1)
  197.             {
  198.                 WaveTableObjectRec*    WaveTableTemp;
  199.  
  200.                 WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
  201.                     WaveTableList->WaveTableArray,Scan);
  202.                 if (HasWaveTableObjectBeenModified(WaveTableTemp))
  203.                     {
  204.                         Flag = True;
  205.                     }
  206.             }
  207.         return Flag;
  208.     }
  209.  
  210.  
  211. /* open an edit window for the selected wave table */
  212. void                                WaveTableListOpenSelection(WaveTableListRec* WaveTableList)
  213.     {
  214.         ArrayRec*                    ListOfSelections;
  215.  
  216.         CheckPtrExistence(WaveTableList);
  217.         ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
  218.         if (ListOfSelections != NIL)
  219.             {
  220.                 long                            Scan;
  221.                 long                            Limit;
  222.  
  223.                 Limit = ArrayGetLength(ListOfSelections);
  224.                 for (Scan = 0; Scan < Limit; Scan += 1)
  225.                     {
  226.                         WaveTableObjectRec*        WaveTableTemp;
  227.  
  228.                         WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,Scan);
  229.                         WaveTableObjectOpenWindow(WaveTableTemp);
  230.                     }
  231.                 DisposeArray(ListOfSelections);
  232.             }
  233.     }
  234.  
  235.  
  236. /* create a new wave table and open a window for it */
  237. void                                WaveTableListNewWaveTable(WaveTableListRec* WaveTableList)
  238.     {
  239.         WaveTableObjectRec*    WaveTable;
  240.  
  241.         CheckPtrExistence(WaveTableList);
  242.         /* create the object */
  243.         WaveTable = NewWaveTableObject(WaveTableList->CodeCenter,
  244.             WaveTableList->MainWindow,WaveTableList);
  245.         if (WaveTable == NIL)
  246.             {
  247.              FailurePoint1:
  248.                 AlertHalt("There is not enough memory available to "
  249.                     "create a new wave table.",NIL);
  250.                 return;
  251.             }
  252.         /* add it to the string list */
  253.         if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTable,True))
  254.             {
  255.              FailurePoint2:
  256.                 DisposeWaveTableObject(WaveTable);
  257.                 goto FailurePoint1;
  258.             }
  259.         MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
  260.         SelectStringListElement(WaveTableList->List,WaveTable);
  261.         MakeStringListSelectionVisible(WaveTableList->List);
  262.         /* add it to the array */
  263.         if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTable))
  264.             {
  265.              FailurePoint3:
  266.                 RemoveStringListElement(WaveTableList->List,WaveTable,True);
  267.                 goto FailurePoint2;
  268.             }
  269.         /* update our internal flags */
  270.         WaveTableList->WaveTableListChanged = True;
  271.         /* change the name in the list */
  272.         WaveTableListWaveTableNameChanged(WaveTableList,WaveTable);
  273.         /* show the window */
  274.         WaveTableObjectOpenWindow(WaveTable);
  275.     }
  276.  
  277.  
  278. /* delete the selected wave table */
  279. void                                WaveTableListDeleteSelection(WaveTableListRec* WaveTableList)
  280.     {
  281.         ArrayRec*                    ListOfSelections;
  282.  
  283.         CheckPtrExistence(WaveTableList);
  284.         ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
  285.         if (ListOfSelections != NIL)
  286.             {
  287.                 long                                Scan;
  288.                 long                                Limit;
  289.  
  290.                 Limit = ArrayGetLength(ListOfSelections);
  291.                 for (Scan = 0; Scan < Limit; Scan += 1)
  292.                     {
  293.                         WaveTableObjectRec*    OneToZap;
  294.  
  295.                         OneToZap = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,Scan);
  296.                         WaveTableListDeleteWaveTable(WaveTableList,OneToZap);
  297.                     }
  298.                 DisposeArray(ListOfSelections);
  299.             }
  300.     }
  301.  
  302.  
  303. /* delete the explicitly specified wave table */
  304. void                                WaveTableListDeleteWaveTable(WaveTableListRec* WaveTableList,
  305.                                             struct WaveTableObjectRec* TheWaveTable)
  306.     {
  307.         long                                Scan;
  308.         long                                Limit;
  309.  
  310.         CheckPtrExistence(WaveTableList);
  311.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  312.         for (Scan = 0; Scan < Limit; Scan += 1)
  313.             {
  314.                 WaveTableObjectRec*        WaveTableTemp;
  315.  
  316.                 WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(
  317.                     WaveTableList->WaveTableArray,Scan);
  318.                 if (TheWaveTable == WaveTableTemp)
  319.                     {
  320.                         FileSpec*                    BackupFileWhere;
  321.                         FileType*                    BackupFile;
  322.                         MyBoolean                    Success = False;
  323.  
  324.                         BackupFileWhere = NewTempFileSpec(CODE4BYTES('?','?','?','?'),
  325.                             CODE4BYTES('?','?','?','?'));
  326.                         if (BackupFileWhere != NIL)
  327.                             {
  328.                                 if (OpenFile(BackupFileWhere,&BackupFile,eReadAndWrite))
  329.                                     {
  330.                                         BufferedOutputRec*    Output;
  331.  
  332.                                         Output = NewBufferedOutput(BackupFile);
  333.                                         if (Output != NIL)
  334.                                             {
  335.                                                 if (WriteBufferedOutput(Output,sizeof(MAGICSCRAPSTRING),
  336.                                                     MAGICSCRAPSTRING))
  337.                                                     {
  338.                                                         if (WaveTableObjectWriteDataOut(TheWaveTable,Output)
  339.                                                             == eFileLoadNoError)
  340.                                                             {
  341.                                                                 Success = True;
  342.                                                             }
  343.                                                     }
  344.                                                 if (!EndBufferedOutput(Output))
  345.                                                     {
  346.                                                         Success = False;
  347.                                                     }
  348.                                             }
  349.                                          else
  350.                                             {
  351.                                                 CloseFile(BackupFile);
  352.                                             }
  353.                                     }
  354.                                  else
  355.                                     {
  356.                                         DeleteFile(BackupFileWhere);
  357.                                         DisposeFileSpec(BackupFileWhere);
  358.                                     }
  359.                             }
  360.                         if (Success)
  361.                             {
  362.                                 MainWindowNewDeleteUndoInfo(WaveTableList->MainWindow,BackupFileWhere,
  363.                                     BackupFile);
  364.                                 DisposeWaveTableObject(WaveTableTemp);
  365.                                 RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
  366.                                 ArrayDeleteElement(WaveTableList->WaveTableArray,Scan);
  367.                                 WaveTableList->WaveTableListChanged = True;
  368.                             }
  369.                          else
  370.                             {
  371.                                 YesNoCancelType        Decision;
  372.  
  373.                                 Decision = AskYesNoCancel("Unable to save undo information for object.  "
  374.                                     "Delete object anyway?",NIL,"Delete","Cancel",NIL/*nothirdbutton*/);
  375.                                 if (Decision == eYes)
  376.                                     {
  377.                                         DisposeWaveTableObject(WaveTableTemp);
  378.                                         RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
  379.                                         ArrayDeleteElement(WaveTableList->WaveTableArray,Scan);
  380.                                         WaveTableList->WaveTableListChanged = True;
  381.                                     }
  382.                             }
  383.                         return;
  384.                     }
  385.             }
  386.         EXECUTE(PRERR(AllowResume,"WaveTableListDeleteWaveTable:  couldn't find object"));
  387.     }
  388.  
  389.  
  390. /* the name of a wave table has changed, so the name in the scrolling */
  391. /* list must also be changed */
  392. void                                WaveTableListWaveTableNameChanged(WaveTableListRec* WaveTableList,
  393.                                             struct WaveTableObjectRec* TheWaveTable)
  394.     {
  395.         char*                            WaveTableName;
  396.  
  397.         CheckPtrExistence(WaveTableList);
  398.         CheckPtrExistence(TheWaveTable);
  399.         ERROR(ArrayFindElement(WaveTableList->WaveTableArray,TheWaveTable) < 0,
  400.             PRERR(ForceAbort,"WaveTableListWaveTableNameChanged:  unknown wave table"));
  401.         WaveTableName = WaveTableObjectGetNameCopy(TheWaveTable);
  402.         if (WaveTableName != NIL)
  403.             {
  404.                 char*                            WaveTableNameNullTerminated;
  405.  
  406.                 WaveTableNameNullTerminated = BlockToStringCopy(WaveTableName);
  407.                 if (WaveTableNameNullTerminated != NIL)
  408.                     {
  409.                         ChangeStringListElementName(WaveTableList->List,
  410.                             WaveTableNameNullTerminated,TheWaveTable);
  411.                         ReleasePtr(WaveTableNameNullTerminated);
  412.                     }
  413.                 ReleasePtr(WaveTableName);
  414.             }
  415.     }
  416.  
  417.  
  418. /* look for a specified wave table.  returns NIL if not found.  the name is NOT null */
  419. /* terminated */
  420. struct WaveTableObjectRec*    WaveTableListLookupNamedWaveTable(
  421.                                             WaveTableListRec* WaveTableList, char* Name)
  422.     {
  423.         long                            Scan;
  424.         long                            Limit;
  425.  
  426.         CheckPtrExistence(WaveTableList);
  427.         CheckPtrExistence(Name);
  428.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  429.         for (Scan = 0; Scan < Limit; Scan += 1)
  430.             {
  431.                 WaveTableObjectRec*        WaveTable;
  432.                 char*                                    NameCopy;
  433.  
  434.                 WaveTable = (WaveTableObjectRec*)ArrayGetElement(
  435.                     WaveTableList->WaveTableArray,Scan);
  436.                 NameCopy = WaveTableObjectGetNameCopy(WaveTable);
  437.                 if (NameCopy != NIL)
  438.                     {
  439.                         if (PtrSize(Name) == PtrSize(NameCopy))
  440.                             {
  441.                                 if (MemEqu(Name,NameCopy,PtrSize(Name)))
  442.                                     {
  443.                                         ReleasePtr(NameCopy);
  444.                                         return WaveTable;
  445.                                     }
  446.                             }
  447.                         ReleasePtr(NameCopy);
  448.                     }
  449.             }
  450.         return NIL;
  451.     }
  452.  
  453.  
  454. /* the document's name changed, so the windows need to be updated */
  455. void                                WaveTableListGlobalNameChange(WaveTableListRec* WaveTableList,
  456.                                             char* NewFilename)
  457.     {
  458.         long                            Scan;
  459.         long                            Limit;
  460.  
  461.         CheckPtrExistence(WaveTableList);
  462.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  463.         for (Scan = 0; Scan < Limit; Scan += 1)
  464.             {
  465.                 WaveTableObjectRec*        WaveTable;
  466.  
  467.                 WaveTable = (WaveTableObjectRec*)ArrayGetElement(
  468.                     WaveTableList->WaveTableArray,Scan);
  469.                 WaveTableObjectGlobalNameChange(WaveTable,NewFilename);
  470.             }
  471.     }
  472.  
  473.  
  474. /* use the provided data to open a new wave table with the specified attributes. */
  475. /* this is used when opening an algorithmic wave table as a data wave table. */
  476. /* RawData MUST be valid. */
  477. WaveTableObjectRec*    WaveTableListCopyRawWaveTableAndOpen(WaveTableListRec* WaveTableList,
  478.                                             char* RawData, NumBitsType NumBits, long NumTables,
  479.                                             long FramesPerTable)
  480.     {
  481.         WaveTableObjectRec*    WaveTable;
  482.  
  483.         CheckPtrExistence(WaveTableList);
  484.         CheckPtrExistence(RawData);
  485.         /* create the object */
  486.         WaveTable = NewWaveTableObjectFromData(WaveTableList->CodeCenter,
  487.             WaveTableList->MainWindow,WaveTableList,RawData,NumBits,NumTables,FramesPerTable);
  488.         if (WaveTable == NIL)
  489.             {
  490.              FailurePoint1:
  491.                 AlertHalt("There is not enough memory available to create a new wave table.",NIL);
  492.                 return NIL;
  493.             }
  494.         /* add it to the string list */
  495.         if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTable,True))
  496.             {
  497.              FailurePoint2:
  498.                 DisposeWaveTableObject(WaveTable);
  499.                 goto FailurePoint1;
  500.             }
  501.         MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
  502.         SelectStringListElement(WaveTableList->List,WaveTable);
  503.         MakeStringListSelectionVisible(WaveTableList->List);
  504.         /* add it to the array */
  505.         if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTable))
  506.             {
  507.              FailurePoint3:
  508.                 RemoveStringListElement(WaveTableList->List,WaveTable,True);
  509.                 goto FailurePoint2;
  510.             }
  511.         /* update our internal flags */
  512.         WaveTableList->WaveTableListChanged = True;
  513.         /* change the name in the list */
  514.         WaveTableListWaveTableNameChanged(WaveTableList,WaveTable);
  515.         /* show the window */
  516.         WaveTableObjectOpenWindow(WaveTable);
  517.         return WaveTable;
  518.     }
  519.  
  520.  
  521. /* get the frame count for the named wave table */
  522. SampleErrors                WaveTableListGetWaveTableFrameCount(WaveTableListRec* WaveTableList,
  523.                                             char* WaveName, long* FrameCountOut)
  524.     {
  525.         WaveTableObjectRec*    WaveTable;
  526.  
  527.         CheckPtrExistence(WaveTableList);
  528.         ERROR(FrameCountOut == NIL,PRERR(ForceAbort,
  529.             "WaveTableListGetWaveTableFrameCount:  frame count out is NIL"));
  530.         WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
  531.         if (WaveTable == NIL)
  532.             {
  533.                 return eEvalSampleUndefined;
  534.             }
  535.         *FrameCountOut = WaveTableObjectEntriesPerTable(WaveTable);
  536.         return eEvalSampleNoError;
  537.     }
  538.  
  539.  
  540. /* get the table count for the named wave table */
  541. SampleErrors                WaveTableListGetWaveTableTableCount(WaveTableListRec* WaveTableList,
  542.                                             char* WaveName, long* TableCountOut)
  543.     {
  544.         WaveTableObjectRec*    WaveTable;
  545.  
  546.         CheckPtrExistence(WaveTableList);
  547.         ERROR(TableCountOut == NIL,PRERR(ForceAbort,
  548.             "WaveTableListGetWaveTableTableCount:  table count out is NIL"));
  549.         WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
  550.         if (WaveTable == NIL)
  551.             {
  552.                 return eEvalSampleUndefined;
  553.             }
  554.         *TableCountOut = WaveTableObjectGetNumTables(WaveTable);
  555.         return eEvalSampleNoError;
  556.     }
  557.  
  558.  
  559. /* get the table array for the named wave table */
  560. SampleErrors                WaveTableListGetWaveTableArray(WaveTableListRec* WaveTableList,
  561.                                             char* WaveName, largefixedsigned** TableOut)
  562.     {
  563.         WaveTableObjectRec*    WaveTable;
  564.  
  565.         CheckPtrExistence(WaveTableList);
  566.         ERROR(TableOut == NIL,PRERR(ForceAbort,
  567.             "WaveTableListGetWaveTableArray:  table out is NIL"));
  568.         WaveTable = WaveTableListLookupNamedWaveTable(WaveTableList,WaveName);
  569.         if (WaveTable == NIL)
  570.             {
  571.                 return eEvalSampleUndefined;
  572.             }
  573.         *TableOut = WaveTableObjectGetFixed(WaveTable);
  574.         return eEvalSampleNoError;
  575.     }
  576.  
  577.  
  578. /*   4-byte little endian number of wave table objects (positive 2's complement) */
  579. /*   n-bytes data for the wave table objects */
  580.  
  581.  
  582. /* read wave table objects from a file.  returns True if completely successful. */
  583. FileLoadingErrors        WaveTableListReadData(WaveTableListRec* WaveTableList,
  584.                                             struct BufferedInputRec* Input)
  585.     {
  586.         signed long                ObjectCount;
  587.         long                            Scan;
  588.  
  589.         CheckPtrExistence(WaveTableList);
  590.         CheckPtrExistence(Input);
  591.  
  592.         /*   4-byte little endian number of objects (positive 2's complement) */
  593.         if (!ReadBufferedSignedLongLittleEndian(Input,&ObjectCount))
  594.             {
  595.                 return eFileLoadDiskError;
  596.             }
  597.         if (ObjectCount < 0)
  598.             {
  599.                 return eFileLoadBadFormat;
  600.             }
  601.  
  602.         /* load the objects */
  603.         for (Scan = 0; Scan < ObjectCount; Scan += 1)
  604.             {
  605.                 WaveTableObjectRec*    WaveTableTemp EXECUTE(= (WaveTableObjectRec*)0x81818181);
  606.                 FileLoadingErrors        Error;
  607.  
  608.                 /* load the object */
  609.                 Error = WaveTableObjectNewFromFile(&WaveTableTemp,Input,
  610.                     WaveTableList->CodeCenter,WaveTableList->MainWindow,WaveTableList);
  611.                 if (Error != eFileLoadNoError)
  612.                     {
  613.                      FailurePoint1:
  614.                         return Error;
  615.                     }
  616.                 /* add it to the string list */
  617.                 if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTableTemp,True))
  618.                     {
  619.                      FailurePoint2:
  620.                         DisposeWaveTableObject(WaveTableTemp);
  621.                         Error = eFileLoadOutOfMemory;
  622.                         goto FailurePoint1;
  623.                     }
  624.                 /* add it to the array */
  625.                 if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTableTemp))
  626.                     {
  627.                      FailurePoint3:
  628.                         RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
  629.                         goto FailurePoint2;
  630.                     }
  631.                 /* change the name in the list */
  632.                 WaveTableListWaveTableNameChanged(WaveTableList,WaveTableTemp);
  633.             }
  634.  
  635.         return eFileLoadNoError;
  636.     }
  637.  
  638.  
  639. /* write wave table objects to a file.  returns True if completely successful. */
  640. FileLoadingErrors        WaveTableListWriteData(WaveTableListRec* WaveTableList,
  641.                                             struct BufferedOutputRec* Output)
  642.     {
  643.         long                            NumberOfObjects;
  644.         long                            Scan;
  645.  
  646.         CheckPtrExistence(Output);
  647.         CheckPtrExistence(WaveTableList);
  648.  
  649.         /*   4-byte little endian number of objects (positive 2's complement) */
  650.         NumberOfObjects = ArrayGetLength(WaveTableList->WaveTableArray);
  651.         if (!WriteBufferedSignedLongLittleEndian(Output,NumberOfObjects))
  652.             {
  653.                 return eFileLoadDiskError;
  654.             }
  655.  
  656.         /* write out the objects */
  657.         for (Scan = 0; Scan < NumberOfObjects; Scan += 1)
  658.             {
  659.                 WaveTableObjectRec*        WaveTable;
  660.                 FileLoadingErrors            Error;
  661.  
  662.                 /* get the object */
  663.                 WaveTable = (WaveTableObjectRec*)ArrayGetElement(
  664.                     WaveTableList->WaveTableArray,Scan);
  665.                 /* write it out */
  666.                 Error = WaveTableObjectWriteDataOut(WaveTable,Output);
  667.                 /* handle errors */
  668.                 if (Error != eFileLoadNoError)
  669.                     {
  670.                         return Error;
  671.                     }
  672.             }
  673.  
  674.         return eFileLoadNoError;
  675.     }
  676.  
  677.  
  678. /* after a file has been saved, this is called to mark all objects as not modified. */
  679. void                                WaveTableListMarkAllObjectsSaved(WaveTableListRec* WaveTableList)
  680.     {
  681.         long                            Scan;
  682.         long                            Limit;
  683.  
  684.         CheckPtrExistence(WaveTableList);
  685.         Limit = ArrayGetLength(WaveTableList->WaveTableArray);
  686.         for (Scan = 0; Scan < Limit; Scan += 1)
  687.             {
  688.                 WaveTableObjectRec*        WaveTable;
  689.  
  690.                 WaveTable = (WaveTableObjectRec*)ArrayGetElement(
  691.                     WaveTableList->WaveTableArray,Scan);
  692.                 WaveTableObjectMarkAsSaved(WaveTable);
  693.             }
  694.         WaveTableList->WaveTableListChanged = False;
  695.     }
  696.  
  697.  
  698. /* copy the selected object in the list to the clipboard.  return False if failed. */
  699. MyBoolean                        WaveTableListCopyObject(WaveTableListRec* WaveTableList)
  700.     {
  701.         ArrayRec*                            ListOfSelections;
  702.         MyBoolean                            TotalSuccessFlag = False;
  703.  
  704.         CheckPtrExistence(WaveTableList);
  705.         ListOfSelections = GetListOfSelectedItems(WaveTableList->List);
  706.         if (ListOfSelections != NIL)
  707.             {
  708.                 if (ArrayGetLength(ListOfSelections) >= 1)
  709.                     {
  710.                         WaveTableObjectRec*    WaveTableTemp;
  711.                         FileSpec*                        TempFileLocation;
  712.  
  713.                         WaveTableTemp = (WaveTableObjectRec*)ArrayGetElement(ListOfSelections,0);
  714.                         /* open the temporary file */
  715.                         TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
  716.                             CODE4BYTES('\?','\?','\?','\?'));
  717.                         if (TempFileLocation != NIL)
  718.                             {
  719.                                 FileType*                            FileDescriptor;
  720.  
  721.                                 if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
  722.                                     {
  723.                                         BufferedOutputRec*        BufferedFile;
  724.  
  725.                                         BufferedFile = NewBufferedOutput(FileDescriptor);
  726.                                         if (BufferedFile != NIL)
  727.                                             {
  728.                                                 MyBoolean                            WriteSucceeded = False;
  729.  
  730.                                                 if (WriteBufferedOutput(BufferedFile,sizeof(MAGICSCRAPSTRING),
  731.                                                     MAGICSCRAPSTRING))
  732.                                                     {
  733.                                                         if (WaveTableObjectWriteDataOut(WaveTableTemp,BufferedFile)
  734.                                                             == eFileLoadNoError)
  735.                                                             {
  736.                                                                 WriteSucceeded = True;
  737.                                                             }
  738.                                                     }
  739.                                                 if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
  740.                                                     {
  741.                                                         char*                            Buffer;
  742.                                                         long                            NumberOfBytes;
  743.  
  744.                                                         NumberOfBytes = GetFileLength(FileDescriptor);
  745.                                                         Buffer = AllocPtrCanFail(NumberOfBytes,
  746.                                                             "WaveTableListCopyObject:  scrap buffer");
  747.                                                         if (Buffer != NIL)
  748.                                                             {
  749.                                                                 if (SetFilePosition(FileDescriptor,0))
  750.                                                                     {
  751.                                                                         if (0 == ReadFromFile(FileDescriptor,
  752.                                                                             Buffer,NumberOfBytes))
  753.                                                                             {
  754.                                                                                 if (SetScrapToThis(Buffer))
  755.                                                                                     {
  756.                                                                                         TotalSuccessFlag = True;
  757.                                                                                     }
  758.                                                                             }
  759.                                                                     }
  760.                                                                 ReleasePtr(Buffer);
  761.                                                             }
  762.                                                     }
  763.                                             }
  764.                                         CloseFile(FileDescriptor);
  765.                                     }
  766.                                 DeleteFile(TempFileLocation);
  767.                                 DisposeFileSpec(TempFileLocation);
  768.                             }
  769.                     }
  770.                 DisposeArray(ListOfSelections);
  771.             }
  772.         return TotalSuccessFlag;
  773.     }
  774.  
  775.  
  776. /* try to paste the clipboard in as a wave table object.  returns False if */
  777. /* it failed or the clipboard did not contain a wave table object. */
  778. MyBoolean                        WaveTableListPasteObject(WaveTableListRec* WaveTableList)
  779.     {
  780.         MyBoolean                    TotalSuccessFlag = False;
  781.         char*                            Scrap;
  782.  
  783.         CheckPtrExistence(WaveTableList);
  784.         Scrap = GetCopyOfScrap();
  785.         if (Scrap != NIL)
  786.             {
  787.                 FileSpec*                    TempFileLocation;
  788.  
  789.                 TempFileLocation = NewTempFileSpec(CODE4BYTES('\?','\?','\?','\?'),
  790.                     CODE4BYTES('\?','\?','\?','\?'));
  791.                 if (TempFileLocation != NIL)
  792.                     {
  793.                         FileType*                            FileDescriptor;
  794.  
  795.                         if (OpenFile(TempFileLocation,&FileDescriptor,eReadAndWrite))
  796.                             {
  797.                                 BufferedOutputRec*        BufferedFile;
  798.  
  799.                                 BufferedFile = NewBufferedOutput(FileDescriptor);
  800.                                 if (BufferedFile != NIL)
  801.                                     {
  802.                                         MyBoolean                            WriteSucceeded = False;
  803.  
  804.                                         if (WriteBufferedOutput(BufferedFile,PtrSize(Scrap),Scrap))
  805.                                             {
  806.                                                 WriteSucceeded = True;
  807.                                             }
  808.                                         if (EndBufferedOutput(BufferedFile) && WriteSucceeded)
  809.                                             {
  810.                                                 TotalSuccessFlag = WaveTableListPasteFromFile(
  811.                                                     WaveTableList,FileDescriptor);
  812.                                             }
  813.                                     }
  814.                                 CloseFile(FileDescriptor);
  815.                             }
  816.                         DeleteFile(TempFileLocation);
  817.                         DisposeFileSpec(TempFileLocation);
  818.                     }
  819.                 ReleasePtr(Scrap);
  820.             }
  821.         return TotalSuccessFlag;
  822.     }
  823.  
  824.  
  825. /* try to paste the wave table object in from the file */
  826. MyBoolean                        WaveTableListPasteFromFile(WaveTableListRec* WaveTableList,
  827.                                             struct FileType* File)
  828.     {
  829.         MyBoolean                    TotalSuccessFlag = False;
  830.  
  831.         CheckPtrExistence(WaveTableList);
  832.         if (SetFilePosition(File,0))
  833.             {
  834.                 BufferedInputRec*    InputFile;
  835.  
  836.                 InputFile = NewBufferedInput(File);
  837.                 if (InputFile != NIL)
  838.                     {
  839.                         char                            HeaderTest[sizeof(MAGICSCRAPSTRING)];
  840.  
  841.                         if (ReadBufferedInput(InputFile,sizeof(MAGICSCRAPSTRING),HeaderTest))
  842.                             {
  843.                                 if (MemEqu(MAGICSCRAPSTRING,HeaderTest,sizeof(MAGICSCRAPSTRING)))
  844.                                     {
  845.                                         WaveTableObjectRec*        WaveTableTemp EXECUTE(= (WaveTableObjectRec*)0x81818181);
  846.  
  847.                                         if (eFileLoadNoError == WaveTableObjectNewFromFile(&WaveTableTemp,InputFile,
  848.                                             WaveTableList->CodeCenter,WaveTableList->MainWindow,WaveTableList))
  849.                                             {
  850.                                                 CheckPtrExistence(WaveTableTemp);
  851.                                                 /* add it to the scrolling object list */
  852.                                                 if (!InsertStringListElement(WaveTableList->List,NIL,NIL,WaveTableTemp,True))
  853.                                                     {
  854.                                                      FailurePoint:
  855.                                                         DisposeWaveTableObject(WaveTableTemp);
  856.                                                     }
  857.                                                  else
  858.                                                     {
  859.                                                         MainWindowDeselectAllOtherStringLists(WaveTableList->MainWindow,WaveTableList);
  860.                                                         SelectStringListElement(WaveTableList->List,WaveTableTemp);
  861.                                                         MakeStringListSelectionVisible(WaveTableList->List);
  862.                                                         /* add it to the array */
  863.                                                         if (!ArrayAppendElement(WaveTableList->WaveTableArray,WaveTableTemp))
  864.                                                             {
  865.                                                                 RemoveStringListElement(WaveTableList->List,WaveTableTemp,True);
  866.                                                                 goto FailurePoint;
  867.                                                             }
  868.                                                          else
  869.                                                             {
  870.                                                                 /* change the name in the list */
  871.                                                                 WaveTableListWaveTableNameChanged(WaveTableList,WaveTableTemp);
  872.                                                                 TotalSuccessFlag = True;
  873.                                                                 WaveTableList->WaveTableListChanged = True;
  874.                                                             }
  875.                                                     }
  876.                                             }
  877.                                     }
  878.                             }
  879.                         EndBufferedInput(InputFile);
  880.                     }
  881.             }
  882.         return TotalSuccessFlag;
  883.     }
  884.  
  885.  
  886. /* find out how many wave tables there are in this list */
  887. long                                WaveTableListHowMany(WaveTableListRec* WaveTableList)
  888.     {
  889.         CheckPtrExistence(WaveTableList);
  890.         return ArrayGetLength(WaveTableList->WaveTableArray);
  891.     }
  892.  
  893.  
  894. /* get an indexed wave tables from the list */
  895. struct WaveTableObjectRec*    WaveTableListGetIndexedWaveTable(
  896.                                             WaveTableListRec* WaveTableList, long Index)
  897.     {
  898.         WaveTableObjectRec*    WaveTable;
  899.  
  900.         CheckPtrExistence(WaveTableList);
  901.         ERROR((Index < 0) || (Index >= WaveTableListHowMany(WaveTableList)),PRERR(ForceAbort,
  902.             "WaveTableListGetIndexedWaveTable:  index out of range"));
  903.         WaveTable = (WaveTableObjectRec*)ArrayGetElement(WaveTableList->WaveTableArray,Index);
  904.         CheckPtrExistence(WaveTable);
  905.         return WaveTable;
  906.     }
  907.